/**
 * 
 */
package eu.reply.android.flashm.common;

import java.io.UnsupportedEncodingException;
import java.net.URL;
import java.net.URLEncoder;


/**
 * Use an instance of this class to prepare a HTTP Request. Afterward this
 * class is used together with {@link HttpConnection} or {@link HttpAsyncConnection} to fetch
 * a resource or service.
 * @author Cuauhtemoc Delgado
 *
 */
public class HttpRequest {
	/** 
	 * Indicates the kind of HTTP method to be used in a HTTP Request.
	 * 
	 * @author Cuauhtemoc Delgado
	 */
	public enum MessageType{
		/**
		 * HTTP Get Method
		 */
		GET,
		/**
		 * HTTP POST Method
		 */
		POST,
		/**
		 * HTTP PUT Method
		 */
		PUT,
		/**
		 * HTTP DELETE Method
		 */
		DELETE;
	}

	
	private static final String DEFAULT_CONTENT_TYPE = "application/x-www-form-urlencoded"; 
	URL url;
	MessageType messageType;
    byte[] content;
    String contentType;
    String user; 
    String pwd;
    
    /**
     * Constructor to build a basic HTTP request
     * 
     * @param type The type of HTTP message (i.e. MessageType.GET,MessageType.PUT)
     * @param url The targeted URL 
     */
    public HttpRequest(MessageType type,URL url){
    	this.messageType = type;
    	this.url = url;
    }
    
    /**
     * Sets the credentials for a HTTP Basic Authentication. The realm is specified 
     * by the URL used to create and Instance of HttpRequest {@link #HttpRequest(MessageType, URL)}
     * 
     * @param user The user login
     * @param pwd
     */
    public void setBasicAuthCredentials(String user,String pwd){
    	this.user=user;
    	this.pwd=pwd;
    }
    
    
    /**
     * Sets the content to be included in the HTTP message. If no HTTP content-type is
     * specified, the the default content type is used.
     * 
     * @param content An array of bytes to be used as payload of the HTTP message
     * @param contentType The MIME type associated to the content or null. 
     */
    public void setContent(byte []content,String contentType){
    	this.content = content;
    	if(contentType!=null && contentType.length()>0){
    		this.contentType = contentType;
    	}else{
    		this.contentType = DEFAULT_CONTENT_TYPE;
    	}
    }

    /**
     * Sets the content to be included in the HTTP message. The default MIME type will
     * be used.
     * 
     * @param content An array of bytes to be used as payload of the HTTP message
     *  
     * @see #setContentType(String)
     * @see #setContent(byte[], String)
     */
	public void setContent(byte[] content) {
		this.content = content;
	}
	
	/**
     * Get the content of the HTTP message.
     * 
     * @return The content of the HTTP request or null if there is no content specified
     *  
     */
	public byte[] getContent() {
		return content;
	}
	
    /**
     * Returns the URL associated to this request
     * @return the URL of this request
     */
	public URL getUrl() {
		return url;
	}

	/**
     * Sets the URL associated to this request
     * 
     */
	public void setUrl(URL url) {
		this.url = url;
	}

	/**
	 * Indicates the kind of HTTP method to be used in a HTTP Request
	 * 
	 * @return The HTTP method 
	 */
	public MessageType getMessageType() {
		return messageType;
	}
	
	/**
     * Get the MIME type associated to the HTTP content
     * 
     * @return The MIME type of the HTTP payload
     */
	public String getContentType() {
		return contentType;
	}

	/**
     * Set the MIME type associated to the HTTP content
     * 
     */
	public void setContentType(String contentType) {
		this.contentType = contentType;
	}

	/**
	 * Get the user name specified for the Basic HTTP Authentication
	 * 
	 * @return user name
	 */
	public String getUser() {
		return user;
	}

	/**
	 * Get the password specified for the Basic HTTP Authentication
	 * @return
	 */
	public String getPwd() {
		return pwd;
	}
    
    
    public static String encodeUrl(String url) throws UnsupportedEncodingException{
    	return URLEncoder.encode(url,"UTF-8");
    }
    
}
